package cn.zy.sys.controller;

import cn.zy.basic.util.AjaxResult;
import cn.zy.basic.util.PageList;
import cn.zy.sys.domain.Dictionarytype;
import cn.zy.sys.query.DictionarytypeQuery;
import cn.zy.sys.service.DictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类:数据字典类型
 */
@Api(value = "部门的API",description="部门相关的CRUD功能") //打在Controller对Controller做描述
@RestController //前端都要发送异步请求,需要加上@ResponseBody注解,所有使用复合注解@RestController
@RequestMapping("/dictionarytype")
public class DictionarytypeController {
    @Autowired
    private DictionarytypeService dictionarytypeService;

    /**
     * 接口:查询单个
     */
    @ApiOperation(value = "接口:通过ID查询" ) //打在方法上，对方法做描述
    @GetMapping("/{id}")
    public Dictionarytype findOne(@PathVariable("id") Long id){
        return dictionarytypeService.loadById(id);
    }

    /**
     * 接口:查询所有
     * @return
     */
    @ApiOperation(value = "接口:查询所有" )
    @GetMapping
    public List<Dictionarytype> findAll(){
        return dictionarytypeService.loadAll();
    }

    /**
     * 接口:删除
     * @param id
     * @return
     */
    @ApiOperation(value = "接口:删除" )
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionarytypeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败!");
        }
    }

    /**
     * 接口:添加和修改
     * @param dictionarytype
     * @return
     */
    @ApiOperation(value = "接口:添加和修改" )
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype){
        try {
            if(dictionarytype.getId()==null){//添加
                dictionarytypeService.save(dictionarytype);
            }else {
                dictionarytypeService.update(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败!");
        }
    }

    /**
     * 接口:分页和高级查询
     * @param dictionarytypeQuery
     * @return
     */
    @ApiOperation(value = "接口:分页和高级查询" )
    @PostMapping
    public PageList<Dictionarytype> queryPage(@RequestBody DictionarytypeQuery dictionarytypeQuery){
        return dictionarytypeService.queryPage(dictionarytypeQuery);
    }

    /**
     * 接口:批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "接口:批量删除" )
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            dictionarytypeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败!");
        }
    }
}
